package com.woshua.customimageview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.woshua.customimageview.R;

public class CustomImageView extends View {

    private static final int TYPE_CIRCLE = 0;
    private static final int TYPE_ROUND = 1;
    private Bitmap mSrc;//图片
    private int type;
    private int mRadius;//圆角大小

    private int mWidth;
    private int mHeight;

    public CustomImageView(Context context) {
        this(context, null);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView,
                defStyleAttr, 0);

        int count = typedArray.getIndexCount();

        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);

            switch (attr) {
                case R.styleable.CustomImageView_src:
                    mSrc = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomImageView_type:
                    type = typedArray.getInt(attr, TYPE_CIRCLE);
                    break;
                case R.styleable.CustomImageView_borderRadius:
                    float dimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics());
                    mRadius = typedArray.getDimensionPixelSize(attr, (int) dimension);
                    break;
                default:
                    break;
            }
        }

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//        super.measure(widthMeasureSpec,heightMeasureSpec);

//        设置宽度
        int spaceMode = MeasureSpec.getMode(widthMeasureSpec);
        int spaceSize = MeasureSpec.getSize(widthMeasureSpec);

        if (spaceMode == MeasureSpec.EXACTLY) {
            mWidth = spaceSize;
        } else {
            //图片决定宽度
            int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth();

            if (spaceMode == MeasureSpec.AT_MOST) {
                mWidth = Math.min(desireByImg, spaceSize);
            } else {
                mWidth = desireByImg;
            }
        }

//        设置高度

        spaceMode = MeasureSpec.getMode(heightMeasureSpec);
        spaceSize = MeasureSpec.getSize(heightMeasureSpec);

        if (spaceMode == MeasureSpec.EXACTLY) {
            mHeight = spaceSize;
        } else {
            //图片决定宽度
            int desireByImg = getPaddingTop() + getPaddingBottom() + mSrc.getHeight();

            if (spaceMode == MeasureSpec.AT_MOST) {
                mHeight = Math.min(desireByImg, spaceSize);
            } else {
                mHeight = desireByImg;
            }
        }

        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (type) {
            case TYPE_CIRCLE:
                int min=Math.min(mWidth,mHeight);

                //如果宽度高度不一致
                mSrc=Bitmap.createScaledBitmap(mSrc,min, min,false);
                canvas.drawBitmap(createCircleImage(mSrc,min),0,0,null);
                break;
            case TYPE_ROUND:
                mSrc=Bitmap.createScaledBitmap(mSrc,mWidth, mHeight,false);
                canvas.drawBitmap(createRoundCornerImage(mSrc),0,0,null);
                break;
            default:
                break;
        }
    }

    /*
    * 创建圆角图片
    * */
    private Bitmap createRoundCornerImage(Bitmap mSrc) {
        Paint paint=new Paint();
        paint.setAntiAlias(true);

        Bitmap result=Bitmap.createBitmap(mWidth,mHeight, Bitmap.Config.ARGB_8888);
        //创建一个同样大小的画布
        Canvas canvas=new Canvas(result);
        RectF rectF=new RectF(0,0,mSrc.getWidth(),mSrc.getHeight());
        canvas.drawRoundRect(rectF,mRadius,mRadius,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        //绘制图片
        canvas.drawBitmap(mSrc,0,0,paint);
        return result;
    }

    /*
    * 根据边长回执圆形图片
    * */
    private Bitmap createCircleImage(Bitmap mSrc, int min) {
        Paint paint=new Paint();
        paint.setAntiAlias(true);

        Bitmap result=Bitmap.createBitmap(min,min, Bitmap.Config.ARGB_8888);
        //创建一个同样大小的画布
        Canvas canvas=new Canvas(result);
        canvas.drawCircle(min/2,min/2,min/2,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        //绘制图片
        canvas.drawBitmap(mSrc,0,0,paint);
        return result;
    }
}
